package com.simtop.controller;

import com.alibaba.fastjson.JSONObject;
import com.simtop.common.ServerResponse;
import com.simtop.pojo.User;
import com.simtop.pojo.resource.Resources;
import com.simtop.pojo.resource.ResourcesConstant;
import com.simtop.service.CheckContent;
import com.simtop.service.ResourcesService;
import com.simtop.util.FileUploadAndDownload;
import com.simtop.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * @author lvyongwei
 */
@RestController
@RequestMapping("/check")
@CrossOrigin
public class CheckController {

    @Value("${file.profile}")
    private String filePath;

    @Autowired
    private FileUploadAndDownload fileUploadAndDownload;

    @Autowired
    private CheckContent checkContentImpl;

    @Autowired
    private ResourcesService resourcesService;

    /**
     * 文件上传审核
     * @param file
     * @return
     */
    @PostMapping("/photo")
    public ServerResponse checkPhoto(MultipartFile file,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        String s = fileUploadAndDownload.upload2(file, "upload");
        /**
         * 设置值
         */
        Resources resources = new Resources();
        resources.setPath(s);
        resources.setName(file.getOriginalFilename());
        resources.setUserId(u.getId());
        resources.setType(ResourcesConstant.PHOTO);
        Float size = Float.parseFloat(String.valueOf(file.getSize())) / 1024;
        BigDecimal ii = new BigDecimal(size);
        // 2表示2位 ROUND_HALF_UP表明四舍五入，
        size = ii.setScale(1, BigDecimal.ROUND_HALF_UP).floatValue();
        resources.setSize(Double.valueOf(size));//单位：kb
        //添加资源内容
        boolean b = resourcesService.insertResources(resources);
        if(!b){
            return ServerResponse.createByErrorMsg("图片资源上传失败");
        }
        //异步审核图片并添加审核结果到表
        checkContentImpl.checkPhoto(filePath + s,resources.getId());
        return ServerResponse.createBySuccessMsg("资源上传成功");
    }

    /**
     * 文本审核
     * @param data
     * @return
     */
    @PostMapping("/text")
    public ServerResponse checkPhoto(@RequestBody String data,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        JSONObject jsonObject = JSONObject.parseObject(data);
        /**
         * 设置值
         */
        Resources resources = new Resources();
        resources.setPath(jsonObject.getString("text"));
        resources.setName(jsonObject.getString("name")+".txt");
        resources.setUserId(u.getId());
        resources.setType(ResourcesConstant.TEXT);
        //添加资源内容
        boolean b = resourcesService.insertResources(resources);
        if(!b){
            return ServerResponse.createByErrorMsg("文本资源上传失败");
        }
        return ServerResponse.createBySuccessMsg("文本资源上传成功");
    }

    /**
     * 音频审核
     * @param file
     * @return
     */
    @PostMapping("/voice")
    public ServerResponse checkVoice(MultipartFile file,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        String s = fileUploadAndDownload.upload2(file, "upload");
        Resources resources = new Resources();
        resources.setPath(s);
        resources.setName(file.getOriginalFilename());
        resources.setUserId(u.getId());
        resources.setType(ResourcesConstant.VOICE);
        Float size = Float.parseFloat(String.valueOf(file.getSize())) / 1048576;
        BigDecimal ii = new BigDecimal(size);
        // 1表示1位 ROUND_HALF_UP表明四舍五入，
        size = ii.setScale(1, BigDecimal.ROUND_HALF_UP).floatValue();
        resources.setSize(Double.valueOf(size));//单位：Mb
        //添加资源
        boolean b = resourcesService.insertResources(resources);
        if(!b){
            return ServerResponse.createByErrorMsg("音频资源上传失败");
        }
        //异步审核音频并添加审核结果到表
        checkContentImpl.checkVoice(filePath + s,resources.getId());
        return ServerResponse.createBySuccessMsg("音频资源上传成功");
    }

    /**
     * 视频审核
     * @return
     */
    @PostMapping("/video")
    public ServerResponse checkVideo(MultipartFile file, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        String s = fileUploadAndDownload.upload2(file, "upload");
        Resources resources = new Resources();
        resources.setPath(s);
        resources.setName(file.getOriginalFilename());
        resources.setUserId(u.getId());
        resources.setType(ResourcesConstant.VIDEO);

        Float size = Float.parseFloat(String.valueOf(file.getSize())) / 1048576;
        BigDecimal ii = new BigDecimal(size);
        // 2表示2位 ROUND_HALF_UP表明四舍五入，
        size = ii.setScale(1, BigDecimal.ROUND_HALF_UP).floatValue();
        resources.setSize(Double.valueOf(size));//单位：Mb
        //添加资源
        Double lastSize = Double.valueOf(size);

        boolean b = resourcesService.insertResources(resources);
        if(!b){
            return ServerResponse.createByErrorMsg("视频资源上传失败");
        }
        //异步审核视频并添加审核结果到表
       checkContentImpl.checkVideo(s,resources.getId(),request , lastSize);

        return ServerResponse.createBySuccessMsg("视频资源上传成功");
    }
}
